package com.jinmdz.fmis.dao.dao.provider;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.ArrayList;

/**
 * 消费费用Provider
 *
 * @author LiCongLu
 * @date 2020-02-13 17:47
 */
public class ChargeItemProvider {

    /**
     * 获取一个查看费用
     *
     * @param id 费用主键
     * @return
     * @author LiCongLu
     * @date 2020-02-13 17:49
     */
    public String getChargeItemById(@Param("id") Integer id) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_item.id,business_code,service_item_id,charge_item.item_price,charge_item.item_number");
                builder.append(" ,charge_item.item_unit,charge_item.item_charge,discount_charge,benefit_charge,reality_charge");
                builder.append(" ,charge_time,as_unsettled,as_settled,charge_settle_id,as_business,as_refund,refund_id,random_code,charge_item.version");
                builder.append(" ,service_item.item_name,service_item.as_fix_price");
                SELECT(builder.toString());
                FROM(" charge_item ");
                JOIN(" service_item on service_item.id = charge_item.service_item_id");
                WHERE(" charge_item.deleted = 0 ");
                WHERE("  charge_item.id = #{id} ");
                ORDER_BY(" charge_item.id limit 1");
            }
        }.toString();
    }

    /**
     * 按照业务编码获取服务项目查看费用
     *
     * @param businessCode     业务编码
     * @param randomCode       随机码
     * @param businessChargeId 业务费用主键
     * @return
     * @author LiCongLu
     * @date 2020-02-13 18:39
     */
    public String listChargeItemByRandomCode(@Param("businessCode") String businessCode, @Param("randomCode") String randomCode, @Param("businessChargeId") Integer businessChargeId) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_item.id,business_code,service_item_id,charge_item.item_price,charge_item.item_number");
                builder.append(" ,charge_item.item_unit,charge_item.item_charge,discount_charge,benefit_charge,reality_charge");
                builder.append(" ,charge_time,as_unsettled,as_settled,charge_settle_id,as_business,as_refund,refund_id,random_code,charge_item.version");
                builder.append(" ,service_item.item_name");
                SELECT(builder.toString());
                FROM(" charge_item ");
                JOIN(" service_item on service_item.id = charge_item.service_item_id ");
                WHERE(" charge_item.deleted = 0 ");
                WHERE(" charge_item.business_code = #{businessCode} ");
                WHERE(" charge_item.random_code = #{randomCode} ");
                WHERE(" charge_item.id != IFNULL(#{businessChargeId},0)");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 批量删除服务项目
     *
     * @param businessCode   业务编码
     * @param ids            服务项目集合
     * @param modifiedUserId 修改帐号
     * @return
     * @author LiCongLu
     * @date 2020-02-14 11:21
     */
    public String deletedForChargeItem(@Param("businessCode") String businessCode, @Param("ids") ArrayList<Integer> ids, @Param("modifiedUserId") Integer modifiedUserId) {
        return new SQL() {
            {
                UPDATE(" charge_item  ");

                SET(" deleted = 1 ");
                SET(" modified_user_id = #{modifiedUserId,jdbcType=INTEGER} ");
                SET(" version = unix_timestamp(now()) ");

                WHERE(" unmodified = 0 and deleted = 0 and as_settled = 0 ");
                WHERE(" business_code = #{businessCode} ");

                // 主键
                String idString = StringUtils.arrayToDelimitedString(ids.toArray(new Integer[]{}), ",");
                WHERE("  id in (" + idString + ")");
            }
        }.toString();
    }

    /**
     * 按照业务编码获取服务项目查看费用
     *
     * @param businessCode 业务编码
     * @return
     * @author LiCongLu
     * @date 2020-02-16 17:15
     */
    public String listChargeItemByBusinessCode(@Param("businessCode") String businessCode) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_item.id,business_code,service_item_id,charge_item.item_price,charge_item.item_number");
                builder.append(" ,charge_item.item_unit,charge_item.item_charge,discount_charge,benefit_charge,reality_charge");
                builder.append(" ,charge_time,as_unsettled,as_settled,charge_settle_id,as_business,as_refund,refund_id,random_code,charge_item.version");
                builder.append(" ,service_item.item_name");
                SELECT(builder.toString());
                FROM(" charge_item ");
                JOIN(" service_item on service_item.id = charge_item.service_item_id ");
                WHERE(" charge_item.deleted = 0 ");
                WHERE(" charge_item.business_code = #{businessCode} ");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 按照业务编码和主键查询服务项目费用
     *
     * @param businessCode
     * @param ids          服务项目集合
     * @return
     * @author LiCongLu
     * @date 2020-02-19 15:33
     */
    public String listChargeItemByIds(@Param("businessCode") String businessCode, @Param("ids") ArrayList<Integer> ids) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_item.id,business_code,service_item_id,charge_item.item_price,charge_item.item_number");
                builder.append(" ,charge_item.item_unit,charge_item.item_charge,discount_charge,benefit_charge,reality_charge");
                builder.append(" ,charge_time,as_unsettled,as_settled,charge_settle_id,as_business,as_refund,refund_id");
                builder.append(" ,charge_item.remark,random_code,charge_item.version");
                builder.append(" ,service_item.item_name");
                SELECT(builder.toString());
                FROM(" charge_item ");
                JOIN(" service_item on service_item.id = charge_item.service_item_id ");
                WHERE(" charge_item.deleted = 0 ");
                WHERE(" charge_item.business_code = #{businessCode} ");
                // 主键
                String idString = StringUtils.arrayToDelimitedString(ids.toArray(new Integer[]{}), ",");
                WHERE("  charge_item.id in (" + idString + ")");
                ORDER_BY(" service_item.sort ");
            }
        }.toString();
    }

    /**
     * 根据结算费用主键ID集合获取费用项目列表数据
     *
     * @param businessCode 业务编码
     * @param settleIds    结算费用主键
     * @return
     * @author ZhangLiRui
     * @date 2020/4/16 17:01
     */
    public String listChargeItemBySettleIds(@Param("businessCode") String businessCode, @Param("settleIds") ArrayList<Integer> settleIds) {
        return new SQL() {
            {
                StringBuilder builder = new StringBuilder();
                builder.append(" charge_item.id,business_code,service_item_id,charge_item.item_price,charge_item.item_number");
                builder.append(" ,charge_item.item_unit,charge_item.item_charge,discount_charge,benefit_charge,reality_charge");
                builder.append(" ,charge_time,as_unsettled,as_settled,charge_settle_id,as_business,as_refund,refund_id,random_code,charge_item.version");
                builder.append(" ,service_item.item_name");
                SELECT(builder.toString());
                FROM(" charge_item ");
                JOIN(" service_item on service_item.id = charge_item.service_item_id");
                WHERE(" charge_item.deleted = 0 and charge_item.as_settled = 1 ");
                WHERE(" charge_item.business_code = #{businessCode} ");
                // 主键
                String idString = StringUtils.arrayToDelimitedString(settleIds.toArray(new Integer[]{}), ",");
                WHERE("  charge_item.charge_settle_id in (" + idString + ")");
                ORDER_BY(" charge_item.id");
            }
        }.toString();
    }

}
